<?php

namespace App\Http\Resources\CruiseCheck;

use App\Models\Cruise\CruiseCheckSelfDetailModel;
use App\Models\Cruise\CruiseCheckSelfTaskCompanyMappingModel;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class SupplierTaskPreviewResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param Request $request
     * @return array|
     */
    public function toArray($request): array
    {

        return $this->cruiseCheckSelfTaskCompanyMappingsRel->map(function (
            CruiseCheckSelfTaskCompanyMappingModel $cruiseCheckSelfTaskCompanyMappingModel
        ) {
            $cruiseCheckSelfTaskRel = $cruiseCheckSelfTaskCompanyMappingModel->cruiseCheckSelfTaskRel;
            $selfDetail             = $this->cruiseCheckSelfDetailRel->where("taskId", $cruiseCheckSelfTaskRel->id)
                                                                     ->where("cruiseCheckCompanyId",
                                                                         $cruiseCheckSelfTaskCompanyMappingModel->cruiseCheckCompanyId)
                                                                     ->where("date", date("Ymd", time()))
                                                                     ->first();

            $status = object_get($selfDetail, "status", CruiseCheckSelfDetailModel::STATUS_WAIT);
//            if (in_array($status,
//                [CruiseCheckSelfDetailModel::STATUS_FINISH, CruiseCheckSelfDetailModel::STATUS_NOT_PRODUCE])) {
//                if (time() > strtotime(formatDateYmd(time()) . " " . $cruiseCheckSelfTaskRel->executeEndHis)) {
//                    $status = CruiseCheckSelfDetailModel::STATUS_INVALID;
//                }
//            }
            $taskStartTimeYmd = formatDateYmd($cruiseCheckSelfTaskRel->taskStartTime);
            $taskEndTimeYmd   = formatDateYmd($cruiseCheckSelfTaskRel->taskEndTime);
            return [
                "cruiseCheckCompanyId" => $cruiseCheckSelfTaskCompanyMappingModel->cruiseCheckCompanyId,
                "selfTask"             => [
                    "id"              => $cruiseCheckSelfTaskRel->id,
                    "name"            => $cruiseCheckSelfTaskRel->name,
                    "rule"            => $cruiseCheckSelfTaskRel->rule,
                    "taskStartTime"   => $taskStartTimeYmd,
                    "taskEndTime"     => $taskEndTimeYmd,
                    "executeStartHis" => $cruiseCheckSelfTaskRel->executeStartHis,
                    "executeEndHis"   => $cruiseCheckSelfTaskRel->executeEndHis,
                    "status"          => (function () use ($taskStartTimeYmd, $taskEndTimeYmd) {
                        $todayYmd = formatDateYmd(time());
                        if ($todayYmd < $taskStartTimeYmd) {
                            return 0;
                        }
                        if ($todayYmd <= $taskEndTimeYmd) {
                            return 1;
                        }
                        return 2;
                    })()
                ],
                "selfDetail"           => [
                    "id"            => object_get($selfDetail, "id", 0),
                    "cruiseOrderId" => object_get($selfDetail, "cruiseOrderId", 0),
                    "status"        => $status,
                    "statusText"    => translateEnumCodeToCn(CruiseCheckSelfDetailModel::STATUS_TEXT, $status),
                ]
            ];
        })->values()->toArray();
    }
}
